package priv.pront.code.national.acwing.graph;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/*spfa*/
public class A851_SPFA求最短路 {
    static int n, m ,idx;
    static int[] dis, w, h, ne, e;
    static boolean[] st;
    static Queue<Integer> queue = new LinkedList<>();
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        dis = new int[m + 10];
        w = new int[m + 10];
        h = new int[n + 10];
        ne = new int[m + 10];
        st  = new boolean[m + 10];
        e = new int[m + 10];
        Arrays.fill(h, -1);
        for(int i = 0; i < m; i++) add(scanner.nextInt(), scanner.nextInt(), scanner.nextInt());
        System.out.println(spfa() == -520 ? "impossible" : spfa());
    }

    private static int spfa(){
        Arrays.fill(dis, 1000000000);
        dis[1] = 0;
        queue.add(1);
        while(!queue.isEmpty()){
            int t = queue.poll();
            st[t] = false;
            for(int i = h[t]; i != -1; i = ne[i]){
                int j = e[i];
                if(dis[j] > dis[t] + w[i]){
                    dis[j] = dis[t] + w[i];
                    if(!st[j]){
                        queue.add(j);
                        st[j] = true;
                    }
                }
            }
        }
        return dis[n] == 1000000000 ? -520 : dis[n];
    }

    private static void add(int a , int b , int weight){
        e[idx] = b; ne[idx] = h[a]; w[idx] = weight; h[a] = idx++;
    }
}
